home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / X11R4 / cmds / X / ddx / Xsun / sunInit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-26  |  18.9 KB  |  690 lines

  1. /*-
  2.  * sunInit.c --
  3.  *    Initialization functions for screen/keyboard/mouse, etc.
  4.  *
  5.  * Copyright (c) 1987 by the Regents of the University of California
  6.  *
  7.  * Permission to use, copy, modify, and distribute this
  8.  * software and its documentation for any purpose and without
  9.  * fee is hereby granted, provided that the above copyright
  10.  * notice appear in all copies.  The University of California
  11.  * makes no representations about the suitability of this
  12.  * software for any purpose.  It is provided "as is" without
  13.  * express or implied warranty.
  14.  *
  15.  *
  16.  */
  17.  
  18. /************************************************************
  19. Copyright 1987 by Sun Microsystems, Inc. Mountain View, CA.
  20.  
  21.                     All Rights Reserved
  22.  
  23. Permission  to  use,  copy,  modify,  and  distribute   this
  24. software  and  its documentation for any purpose and without
  25. fee is hereby granted, provided that the above copyright no-
  26. tice  appear  in all copies and that both that copyright no-
  27. tice and this permission notice appear in  supporting  docu-
  28. mentation,  and  that the names of Sun or MIT not be used in
  29. advertising or publicity pertaining to distribution  of  the
  30. software  without specific prior written permission. Sun and
  31. M.I.T. make no representations about the suitability of this
  32. software for any purpose. It is provided "as is" without any
  33. express or implied warranty.
  34.  
  35. SUN DISCLAIMS ALL WARRANTIES WITH REGARD TO  THIS  SOFTWARE,
  36. INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FIT-
  37. NESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SUN BE  LI-
  38. ABLE  FOR  ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  39. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,  DATA  OR
  40. PROFITS,  WHETHER  IN  AN  ACTION OF CONTRACT, NEGLIGENCE OR
  41. OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION  WITH
  42. THE USE OR PERFORMANCE OF THIS SOFTWARE.
  43.  
  44. ********************************************************/
  45.  
  46. #ifndef    lint
  47. static char sccsid[] = "%W %G Copyright 1987 Sun Micro";
  48. #endif
  49.  
  50. #include    "sun.h"
  51. #include    <signal.h>
  52. #include    <servermd.h>
  53. #include    "dixstruct.h"
  54. #include    "dix.h"
  55. #include    "opaque.h"
  56. #include    "mipointer.h"
  57. #include    <stdio.h>
  58.  
  59. #define    R_OK    4
  60. #define    W_OK    2
  61. #define    X_OK    1
  62. #define    F_OK    0
  63.  
  64. #include "sys/ioctl.h"
  65. #include "fcntl.h"
  66.  
  67. extern int sunMouseProc();
  68. extern int sunKbdProc();
  69. extern Bool sunBW2Probe(), sunBW2Create();
  70. #ifndef    sprite
  71. extern Bool sunCG2CProbe(), sunCG2CCreate();
  72. #endif    sprite
  73. extern Bool sunCG3CProbe(), sunCG3CCreate();
  74. extern Bool sunCG4CProbe(), sunCG4CCreate();
  75. extern Bool sunCG6CProbe(), sunCG6CCreate();
  76. extern void ProcessInputEvents();
  77.  
  78. extern void SetInputCheck();
  79. extern char *strncpy();
  80. extern GCPtr CreateScratchGC();
  81.  
  82. #define    XDEVICE    "XDEVICE"
  83. #define    PARENT    "WINDOW_GFX"
  84.  
  85. int spriteCheckInput = 0;     /* For use with SetInputCheck */
  86. static int autoRepeatHandlersInstalled;    /* FALSE each time InitOutput called */
  87.  
  88. static Bool sunDevsProbed = FALSE;
  89. Bool sunSupportsDepth8 = FALSE;
  90. unsigned long sunGeneration = 0;
  91.  
  92. sunFbDataRec sunFbData[] = {
  93.     sunBW2Probe,      "/dev/bwtwo0",        sunBW2Create,
  94. #ifndef    sprite
  95.     sunCG2CProbe,      "/dev/cgtwo0",        sunCG2CCreate,
  96. #endif    sprite
  97.     sunCG3CProbe,      "/dev/cgthree0",    sunCG3CCreate,
  98.     sunCG6CProbe,    "/dev/cgsix0",        sunCG6CCreate,
  99.     sunCG4CProbe,      "/dev/cgfour0",        sunCG4CCreate,
  100.     sunBW2Probe,      "/dev/bwtwo0",        sunBW2Create,
  101. };
  102.  
  103. /*
  104.  * NUMSCREENS is the number of supported frame buffers (i.e. the number of
  105.  * structures in sunFbData which have an actual probeProc).
  106.  */
  107. #define NUMSCREENS (sizeof(sunFbData)/sizeof(sunFbData[0]))
  108. #define NUMDEVICES  2
  109.  
  110. fbFd sunFbs[NUMDEVICES];
  111.  
  112. static PixmapFormatRec    formats[] = {
  113.     1, 1, BITMAP_SCANLINE_PAD,    /* 1-bit deep */
  114.     8, 8, BITMAP_SCANLINE_PAD,    /* 8-bit deep */
  115. };
  116. #define NUMFORMATS    (sizeof formats)/(sizeof formats[0])
  117.  
  118. /*-
  119.  *-----------------------------------------------------------------------
  120.  * sunNonBlockConsoleOff --
  121.  *    Turn non-blocking mode on the console off, so you don't get logged
  122.  *    out when the server exits.
  123.  *
  124.  * Results:
  125.  *    None.
  126.  *
  127.  * Side Effects:
  128.  *    None.
  129.  *
  130.  *-----------------------------------------------------------------------
  131.  */
  132. /*ARGSUSED*/
  133. sunNonBlockConsoleOff(arg)
  134.     char    *arg;
  135. {
  136.     register int i;
  137.  
  138. #ifndef    sprite
  139.     i = fcntl(2, F_GETFL, 0);
  140.     if (i >= 0)
  141.     (void) fcntl(2, F_SETFL, i & ~FNDELAY);
  142. #endif    sprite
  143. }
  144.  
  145. /*-
  146.  *-----------------------------------------------------------------------
  147.  * InitOutput --
  148.  *    Initialize screenInfo for all actually accessible framebuffers.
  149.  *    The
  150.  *
  151.  * Results:
  152.  *    screenInfo init proc field set
  153.  *
  154.  * Side Effects:
  155.  *    None
  156.  *
  157.  *-----------------------------------------------------------------------
  158.  */
  159.  
  160. InitOutput(pScreenInfo, argc, argv)
  161.     ScreenInfo       *pScreenInfo;
  162.     int           argc;
  163.     char          **argv;
  164. {
  165.     int           i, n, dev;
  166.     int          nonBlockConsole = 1;
  167.     static int      setup_on_exit = 0;
  168.     int          devStart = 1;
  169.  
  170.     if (!monitorResolution)
  171.     monitorResolution = 90;
  172.     for (i = 1; i < argc; i++) {
  173.     if (!strcmp(argv[i],"-debug"))
  174.         nonBlockConsole = 0;
  175.     else if (!strcmp(argv[i],"-mono"))
  176.         devStart = 0;
  177.     }
  178. #ifndef    sprite
  179.     /*
  180.      *    Writes to /dev/console can block - causing an
  181.      *    excess of error messages to hang the server in
  182.      *    deadlock.  So.......
  183.      */
  184.     if (nonBlockConsole) {
  185.     if (!setup_on_exit) {
  186.         if (on_exit(sunNonBlockConsoleOff, (char *)0))
  187.         ErrorF("InitOutput: can't register NBIO exit handler\n");
  188.         setup_on_exit = 1;
  189.     }
  190.     i = fcntl(2, F_GETFL, 0);
  191.     if (i >= 0)
  192.         i = fcntl(2, F_SETFL, i | FNDELAY);
  193.     if (i < 0) {
  194.         perror("fcntl");
  195.         ErrorF("InitOutput: can't put stderr in non-block mode\n");
  196.     }
  197.     }
  198. #endif    sprite
  199.     pScreenInfo->imageByteOrder = IMAGE_BYTE_ORDER;
  200.     pScreenInfo->bitmapScanlineUnit = BITMAP_SCANLINE_UNIT;
  201.     pScreenInfo->bitmapScanlinePad = BITMAP_SCANLINE_PAD;
  202.     pScreenInfo->bitmapBitOrder = BITMAP_BIT_ORDER;
  203.  
  204.     pScreenInfo->numPixmapFormats = NUMFORMATS;
  205.     for (i=0; i< NUMFORMATS; i++)
  206.         pScreenInfo->formats[i] = formats[i];
  207.  
  208.     autoRepeatHandlersInstalled = FALSE;
  209.  
  210.     if (!sunDevsProbed)
  211.     {
  212.     n = 0;
  213.     for (i = NUMSCREENS, dev = devStart; --i > 0; dev++) {
  214.         if ((*sunFbData[dev].probeProc)(pScreenInfo, n, dev, argc, argv))
  215.         n++;
  216.         else
  217.         sunFbData[dev].createProc = NULL;
  218.     }
  219.     sunDevsProbed = TRUE;
  220.     if (n == 0)
  221.         return;
  222.     }
  223.     if (!sunSupportsDepth8)
  224.     pScreenInfo->numPixmapFormats--;
  225.     for (i = NUMSCREENS, dev = devStart; --i > 0; dev++) {
  226.     if (sunFbData[dev].createProc)
  227.         (*sunFbData[dev].createProc)(pScreenInfo, argc, argv);
  228.     }
  229.     sunGeneration = serverGeneration;
  230.     sunInitCursor();
  231.     signal(SIGWINCH, SIG_IGN);
  232. }
  233.  
  234. /*-
  235.  *-----------------------------------------------------------------------
  236.  * InitInput --
  237.  *    Initialize all supported input devices...what else is there
  238.  *    besides pointer and keyboard?
  239.  *
  240.  * Results:
  241.  *    None.
  242.  *
  243.  * Side Effects:
  244.  *    Two DeviceRec's are allocated and registered as the system pointer
  245.  *    and keyboard devices.
  246.  *
  247.  *-----------------------------------------------------------------------
  248.  */
  249. /*ARGSUSED*/
  250. InitInput(argc, argv)
  251.     int           argc;
  252.     char          **argv;
  253. {
  254.     DevicePtr p, k;
  255.     static int  zero = 0;
  256.     
  257.     p = AddInputDevice(sunMouseProc, TRUE);
  258.     k = AddInputDevice(sunKbdProc, TRUE);
  259.     if (!p || !k)
  260.     FatalError("failed to create input devices in InitInput");
  261.  
  262.     RegisterPointerDevice(p);
  263.     RegisterKeyboardDevice(k);
  264.     miRegisterPointerDevice(screenInfo.screens[0], p);
  265.  
  266.     spriteCheckInput = 0;
  267.     SetInputCheck (&zero, &spriteCheckInput);
  268.  
  269.     screenInfo.screens[0]->blockData =
  270.     screenInfo.screens[0]->wakeupData = (pointer)k;
  271. }
  272.  
  273. /*-
  274.  *-----------------------------------------------------------------------
  275.  * sunScreenInit --
  276.  *    Things which must be done for all types of frame buffers...
  277.  *    Should be called last of all.
  278.  *
  279.  * Results:
  280.  *    TRUE if successful, else FALSE
  281.  *
  282.  * Side Effects:
  283.  *    Both a BlockHandler and a WakeupHandler are installed for the
  284.  *    first screen.  Together, these handlers implement autorepeat
  285.  *    keystrokes on the Sun.
  286.  *
  287.  *-----------------------------------------------------------------------
  288.  */
  289. Bool
  290. sunScreenInit (pScreen)
  291.     ScreenPtr      pScreen;
  292. {
  293.     extern void   sunBlockHandler();
  294.     extern void   sunWakeupHandler();
  295.     static ScreenPtr autoRepeatScreen;
  296.     extern miPointerCursorFuncRec   sunPointerCursorFuncs;
  297.  
  298.     /*
  299.      *    Block/Unblock handlers
  300.      */
  301.     if (autoRepeatHandlersInstalled == FALSE) {
  302.     autoRepeatScreen = pScreen;
  303.     autoRepeatHandlersInstalled = TRUE;
  304.     }
  305.  
  306.     if (pScreen == autoRepeatScreen) {
  307.         pScreen->BlockHandler = sunBlockHandler;
  308.         pScreen->WakeupHandler = sunWakeupHandler;
  309.     }
  310.  
  311.     miDCInitialize (pScreen, &sunPointerCursorFuncs);
  312.  
  313. #ifdef SUN_WINDOWS
  314.     /*
  315.      * We need to wrap one of the cursor functions so that
  316.      * we can keep the SunWindows cursor position in sync
  317.      * with X. We still call the mi version to do the real
  318.      * work.
  319.      */
  320.     realSetCursorPosition = pScreen->SetCursorPosition;
  321.     pScreen->SetCursorPosition = sunSetCursorPosition;
  322. #endif
  323.  
  324.     return TRUE;
  325. }
  326.  
  327.  
  328. extern char *getenv();
  329.  
  330. /*-
  331.  *-----------------------------------------------------------------------
  332.  * nthdev --
  333.  *    Return the nth device in a colon-separated list of devices.
  334.  *    n is 0-origin.
  335.  *
  336.  * Results:
  337.  *    A pointer to a STATIC string which is the device name.
  338.  *
  339.  * Side Effects:
  340.  *    None.
  341.  *
  342.  *-----------------------------------------------------------------------
  343.  */
  344. static char *
  345. nthdev (dList, n)
  346.     register char    *dList;        /* Colon-separated device names */
  347.     int        n;              /* Device number wanted */
  348. {
  349.     char *result;
  350.     static char returnstring[100];
  351.  
  352.     while (n--) {
  353.     while (*dList && *dList != ':') {
  354.         dList++;
  355.     }
  356.     }
  357.     if (*dList) {
  358.     register char *cp = dList;
  359.  
  360.     while (*cp && *cp != ':') {
  361.         cp++;
  362.     }
  363.     result = returnstring;
  364.     strncpy (result, dList, cp - dList);
  365.     result[cp - dList] = '\0';
  366.     } else {
  367.     result = (char *)0;
  368.     }
  369.     return (result);
  370. }
  371.  
  372. /*-
  373.  *-----------------------------------------------------------------------
  374.  * sunOpenFrameBuffer --
  375.  *    Open a frame buffer according to several rules. If running under
  376.  *    overview and we're set up for it, use the device given in the
  377.  *    PARENT envariable and note that the screen is under overview.
  378.  *    Else find the device to use by looking in the sunFbData table,
  379.  *    an XDEVICE envariable, a -dev switch or using /dev/fb if trying
  380.  *    to open screen 0 and all else has failed.
  381.  *
  382.  * Results:
  383.  *    The fd of the framebuffer.
  384.  *
  385.  * Side Effects:
  386.  *
  387.  *-----------------------------------------------------------------------
  388.  */
  389. int
  390. sunOpenFrameBuffer(expect, pfbType, index, fbNum, argc, argv)
  391.     int              expect;       /* The expected type of framebuffer */
  392.     struct fbtype *pfbType;     /* Place to store the fb info */
  393.     int              fbNum;        /* Index into the sunFbData array */
  394.     int              index;        /* Screen index */
  395.     int              argc;            /* Command-line arguments... */
  396.     char      **argv;       /* ... */
  397. {
  398.     char             *name=(char *)0;
  399.     int           i;            /* Index into argument list */
  400.     int           fd = -1;            /* Descriptor to device */
  401.     static int      devFbUsed=FALSE;  /* true if /dev/fb has been used for a */
  402.                                   /* screen already */
  403.     static Bool      inited = FALSE;
  404.     static char      *xdevice;     /* string of devices to use from environ */
  405.     static char      *devsw;       /* string of devices from args */
  406.     struct fbgattr  fbattr;
  407.     int          type;
  408.  
  409.     sunFbs[index].parent = FALSE;
  410.  
  411.     if (!inited) {
  412.     xdevice = devsw = (char *)NULL;
  413.  
  414.     xdevice = getenv (XDEVICE);
  415.     /*
  416.      * Look for an argument of the form -dev <device-string>
  417.      * If such a one is found place the <device-string> in devsw.
  418.      */
  419.     for (i = 1; i < argc; i++) {
  420.         if ((strcmp(argv[i], "-dev") == 0) && (i + 1 < argc)) {
  421.         devsw = argv[i+1];
  422.         break;
  423.         }
  424.     }
  425.     inited = TRUE;
  426.     }
  427.  
  428.     /*
  429.      * Attempt to find a file name for the frame buffer 
  430.      */
  431.  
  432.     /*
  433.      * First see if any device was given on the command line.
  434.      * If one was and the device is both readable and writeable,
  435.      * set 'name' to it, else set it to NULL.
  436.      */
  437.     if (devsw == (char *)NULL ||
  438.     (name = nthdev (devsw, index)) == (char *)NULL ||
  439.     (access (name, R_OK | W_OK) != 0) ||
  440.     (strcmp(name, sunFbData[fbNum].devName) != 0)) {
  441.         name = (char *)NULL;
  442.     }
  443.         
  444.     /*
  445.      * If we still don't have a device for this screen, check the
  446.      * environment variable for one. If one was given, stick its
  447.      * path in name and check its accessibility. If it's not
  448.      * properly accessible, then reset the name to NULL to force the
  449.      * checking of the sunFbData array.
  450.      */
  451.     if (devsw == (char *)NULL && name == (char *)NULL &&
  452.     xdevice != (char *)NULL &&
  453.     (name = nthdev(xdevice, index)) != (char *)NULL &&
  454.     (access (name, R_OK | W_OK) != 0)) {
  455.         name = (char *)NULL;
  456.     }
  457.  
  458.     /*
  459.      * Take the device given in the frame buffer description
  460.      * and see if it exists and is accessible. If it does/is,
  461.      * we will use it, as long as no other device was given.
  462.      */
  463.     if (devsw == (char *)NULL && name == (char *)NULL &&
  464.     access(sunFbData[fbNum].devName, (R_OK | W_OK)) == 0) {
  465.         name = sunFbData[fbNum].devName;
  466.     }
  467.  
  468.     /*
  469.      * If we still have nothing and have yet to use "/dev/fb" for
  470.      * a screen, default the name to be "/dev/fb"
  471.      */
  472.     if (devsw == (char *)NULL && name == (char *)NULL && !devFbUsed) {
  473.     name = "/dev/fb";
  474.     }
  475.  
  476.  
  477.     if (name != (char *) NULL && sunUseSunWindows()) {
  478. #ifdef    SUN_WINDOWS
  479.  
  480.     /*
  481.      * Running X in coexistence with SunWindows.
  482.      *
  483.      * This section of code enables X to run with SunWindows.  This is 
  484.      * accomplished by opening up screens and windows in the SunWindows 
  485.      * style and accepting input events from them.  However, since the X 
  486.      * graphics libraries want a raw framebuffer, we open the 
  487.      * framebuffer of the current screen and return that.  So we get 
  488.      * input from SunWindows but send output to the raw framebuffer.
  489.      */
  490.  
  491.     char       *parent = getenv("WINDOW_PARENT");
  492.     struct screen sc;
  493.     int        winFd;
  494.     int        parentFd;
  495.     int        framebufferFd;
  496.     Rect        r;
  497.     static struct screen newScreen;
  498.     struct inputmask inputMask;
  499.     Bool        keepParent = FALSE;
  500.     static int  sunFbFound = 0;    /* True if FB found under SunWindows */
  501.  
  502.     /*
  503.      * If no device was specified on the command line, open the window 
  504.      * specified in WINDOW_PARENT.  If a device was specified, open a 
  505.      * new screen on that device and use it as a parent window.
  506.      */
  507.  
  508.     if ( devsw ) {
  509.         bzero( (caddr_t)&newScreen, sizeof(newScreen) );
  510.         strcpy( newScreen.scr_fbname, name );
  511.         newScreen.scr_flags |= SCR_TOGGLEENABLE;
  512.         if ( (parentFd = win_screennew( &newScreen )) < 0 ) {
  513.         ErrorF( "sunOpenFrameBuffer: Can't open new screen on %s.\n",
  514.             name );
  515.         return( -1 );
  516.         }
  517.         keepParent = TRUE;
  518.     } else {
  519.         if ((parentFd = open(parent, 2, 0)) < 0) {
  520.         ErrorF("sunOpenFrameBuffer: Can't open parent %s.\n", parent);
  521.         return (-1);
  522.         }
  523.     }
  524.  
  525.     if ((winFd = win_getnewwindow()) < 0) {
  526.         ErrorF("sunOpenFrameBuffer: Can't open a new window.\n");
  527.         close( parentFd );
  528.         return (-1);
  529.     }
  530.  
  531.     /* link the new window into the window hierarchy */
  532.     bzero((caddr_t) & r, sizeof r);
  533.     win_getrect(parentFd, &r);
  534.     win_setrect(winFd, &r);
  535.     win_setlink(winFd, WL_PARENT, win_fdtonumber(parentFd));
  536.     win_setlink(winFd, WL_OLDERSIB, win_getlink(parentFd, WL_TOPCHILD));
  537.     sunFbs[index].parent = TRUE;
  538.  
  539.     /*
  540.      * We would like to close the parent window here, since it's no 
  541.      * longer needed.  However, in the case where the parent is really a 
  542.      * screen, we have to keep it open, because closing a screen closes 
  543.      * all the windows on that screen.
  544.      */
  545.  
  546.     if ( ! keepParent )
  547.         close(parentFd);
  548.     
  549.     /*
  550.      * Express interest in SunView mouse events.  Note: the win_insert 
  551.      * must be AFTER setting the input mask in order to receive the 
  552.      * initial LOC_WINENTER event.
  553.      */
  554.  
  555.     input_imnull(&inputMask);
  556.     inputMask.im_flags = IM_ASCII | IM_META | IM_NEGEVENT | IM_INTRANSIT;
  557.  
  558.     win_setinputcodebit(&inputMask, LOC_MOVE);
  559.     win_setinputcodebit(&inputMask, LOC_WINEXIT);
  560.     win_setinputcodebit(&inputMask, LOC_WINENTER);
  561.     win_setinputcodebit(&inputMask, MS_LEFT);
  562.     win_setinputcodebit(&inputMask, MS_MIDDLE);
  563.     win_setinputcodebit(&inputMask, MS_RIGHT);
  564.     win_set_pick_mask(winFd, &inputMask);
  565.  
  566.     input_imnull(&inputMask);
  567.     win_setinputcodebit(&inputMask, KBD_USE);
  568.     win_setinputcodebit(&inputMask, KBD_DONE);
  569.     win_set_kbd_mask(winFd, &inputMask);
  570.  
  571.     win_insert(winFd);
  572.  
  573.     /*
  574.      * Determine the framebuffer name from the window's screen, then 
  575.      * open it.  Then do an FBIOGTYPE ioctl to determine its type.
  576.      */
  577.  
  578.     win_screenget( winFd, &sc );
  579.     if ((framebufferFd = open(sc.scr_fbname, O_RDWR, 0)) < 0) {
  580.         ErrorF("sunOpenFrameBuffer: can't open %s\n",sc.scr_fbname);
  581.         (void) close(winFd);
  582.         (void) close(parentFd);
  583.         return (-1);
  584.     }
  585.  
  586.     if (ioctl(framebufferFd, FBIOGTYPE, pfbType) < 0) {
  587.         perror("sunOpenFrameBuffer: FBIOGTYPE");
  588. badfb:
  589.         (void) close(framebufferFd);
  590.         (void) close(winFd);
  591.         (void) close(parentFd);
  592.         return (-1);
  593.     }
  594.  
  595.     if (pfbType->fb_type != expect) {
  596.         struct fbgattr fbgattr;
  597.         int i;
  598.  
  599.         /*
  600.          * On a 3/110 we only want to open one display
  601.          * on /dev/fb (or /dev/cgfour0) if no device is
  602.          * specified in the command line.  If not, we won't
  603.          * be able to slide back and forth between X displays
  604.          * since only one SunWindows framebuffer is open.
  605.          * The screen that is opened is /dev/bwtwo0.
  606.          */
  607.         if (sunFbFound && (devsw == (char *) NULL))
  608.         goto badfb;
  609.  
  610.         if (ioctl(framebufferFd, FBIOGATTR, &fbgattr) < 0)
  611.         goto badfb;
  612.         
  613.         for (i=0; i<FB_ATTR_NEMUTYPES; i++)
  614.         if (fbgattr.emu_types[i] == expect)
  615.             break;
  616.         else if (fbgattr.emu_types[i] == -1)
  617.             goto badfb;
  618.     }
  619.         /*
  620.      * NDELAY only applies to "input" fds, or fds that can be
  621.      * read.  sunwindows fds are read, while frame buffer fds aren't.
  622.      * That's why this fcntl is in the conditional compilation section.
  623.      */
  624.  
  625.     if (fcntl(winFd, F_SETFL, FNDELAY|FASYNC) < 0) {
  626.         ErrorF("Can't set FNDELAY|FASYNC on %s\n",name);
  627.         perror("sunOpenFrameBuffer");
  628.         (void) close(winFd);
  629.         return (-1);
  630.     }
  631.     
  632.     fd = framebufferFd;
  633.     windowFd = winFd;
  634.     sunFbFound++;
  635. #else
  636.     ErrorF("Not configured to run inside SunWindows\n");
  637.     fd = -1;
  638. #endif    SUN_WINDOWS
  639.     } else if (name) {
  640.     fd = open(name, O_RDWR, 0);
  641.         if (fd < 0) {
  642.         fprintf(stderr, "Open of %s failed:", name);
  643.         perror("");
  644.         return (-1);
  645.     } 
  646.     if (ioctl(fd, FBIOGATTR, &fbattr) < 0)
  647.     {
  648.         if (ioctl(fd, FBIOGTYPE, pfbType) < 0) {
  649.         perror("sunOpenFrameBuffer");
  650.         (void) close(fd);
  651.         return (-1);
  652.         }
  653.         type = pfbType->fb_type;
  654.  
  655.         printf("Framebuffer=|%s|, type = %d\n", name, type);
  656.  
  657.     }
  658.     else
  659.     {
  660.         int    i;
  661.  
  662.         type = fbattr.real_type;
  663.         *pfbType = fbattr.fbtype;
  664.         if (expect == FBTYPE_SUN2BW && expect != type)
  665.         {
  666.             for (i = 0; i < FB_ATTR_NEMUTYPES; i++)
  667.             {
  668.             if (expect == fbattr.emu_types[i])
  669.             {
  670.                 type = fbattr.emu_types[i];
  671.             break;
  672.             }
  673.             }
  674.         }
  675.     }
  676.     /* XXX - this is temporary 'cos the CG4 pretends its a BW2 */
  677.     if (strcmp(name, sunFbData[fbNum].devName) != 0 && type != expect)
  678.     {
  679.         (void) close(fd);
  680.         return (-1);
  681.     }
  682.     }
  683.  
  684.     if (name && strcmp (name, "/dev/fb") == 0) {
  685.     devFbUsed = TRUE;
  686.     }
  687.  
  688.     return (fd);
  689. }
  690.